const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())

const fs = require('fs');

//获取客户端 ip 地址
app.use(function (request, response, next) {
    response.setHeader('Access-Control-Allow-Origin', "*");
    response.setHeader('Access-Control-Allow-Headers', "*");
    response.setHeader('Access-Control-Allow-Methods', "*");
    next();
});

app.all('/server', (req, res) => {
    res.send('这里是服务器');
})

app.all('/delay-server', (req, res) => {
    setTimeout(() => {
        res.send('这里是延时响应的路由');
    }, 3000)
});

app.all('/test', (req, res) => {
    res.send('测试服务');
})

app.all('/movie', (req, res) => {
    res.json([{
        "id": 1,
        "name": "让子弹飞",
        "director": "美文"
      },
      {
        "id": 2,
        "name": "阿凡达",
        "director": "卡梅隆"
      }]);
});

app.all('/music', (req, res) => {
    res.json([{
        "id": 1,
        "name": "你的答案",
        "singer": "阿冗"
      },
      {
        "id": 2,
        "name": "爱你",
        "singer": "王心凌"
      }]);
});

app.all('*', (req, res) => {
    if(req.method.toUpperCase() === 'OPTIONS'){
        res.status(200).end('ok');
        return;
    }
    res.status(404);
    res.end('not found');
})

app.listen(8090, () => {
    console.log('服务已经启动....8090');
});
